﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xiami.Helper.Model;
using AMing.Extension.Net40;
using Html5Music.Method;
using System.Web.Security;

namespace Html5Music.Controllers
{
    public class AdminController : BaseController
    {
        #region Data
        private Data.PlayListData _playlist = null;

        public Data.PlayListData Playlist
        {
            get
            {
                if (_playlist == null)
                {
                    Data.Config.Path_PlayList = Server.MapPath("~/file/playlist.txt");
                    _playlist = new Data.PlayListData();
                }
                return _playlist;
            }
        }
        private Data.UserListData _userlist = null;

        public Data.UserListData Userlist
        {
            get
            {
                if (_userlist == null)
                {
                    Data.Config.Path_UserList = Server.MapPath("~/file/userlist.txt");
                    _userlist = new Data.UserListData();
                }
                return _userlist;
            }
        }
        #endregion

        public AdminController()
        {
            base.ViewPath = "~/Views/Admin/{0}.cshtml";
        }

        //[AdminAuthorize]
        public ActionResult Index()
        {
            var b = HttpContext.User.Identity.IsAuthenticated;
            ViewBag.Access = this.Admin_Access;

            return ViewPage();
        }


        /// <summary>
        /// 登陆页
        /// </summary>
        /// <param name="callback"></param>
        /// <returns></returns>
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return ViewPage();
        }


        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="userpwd"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(string userid, string userpwd, string returnUrl)
        {
            bool isLogin = false;
            if (userid == SuperAdmin_ID && userpwd == SuperAdmin_Pwd)//超级管理员登陆
            {
                isLogin = true;
                string all_access = "";
                foreach (var item in System.Enum.GetValues(typeof(UserAccess)))
                {
                    all_access += "," + item.ToString();
                }
                SetUserAccess(all_access, userid);
            }
            else
            {
                Model.User user = Userlist.GetByLoginName(userid);
                if (user != null && user.Password == userpwd)
                {
                    isLogin = true;
                    SetUserAccess(user.Access, userid);
                }
            }
            if (isLogin)
            {
                FormsAuthentication.SetAuthCookie(userid, true);

                if (string.IsNullOrWhiteSpace(returnUrl))
                {
                    returnUrl = "/admin/index";
                }

                return Redirect(returnUrl);
            }
            ViewBag.UserID = userid;
            ViewBag.Error = "账号密码有误，登陆失败！";

            return Login(returnUrl);
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        [AdminAuthorize]
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            base.Loguot();

            return RedirectToAction("index");
        }


        #region Method
        /// <summary>
        /// 设置用户权限及账号
        /// </summary>
        /// <param name="access"></param>
        /// <param name="username"></param>
        private void SetUserAccess(string access, string username)
        {
            base.Admin_Access = string.Format("userid:{0},{1}", username, access);
        }

        #endregion
    }
}
